#include <stdlib.h>
#include "MediaParser.h"


int MediaParsing(char *filePath,struct Meta* meta)
{

	int ret = -1;
	char FileName[256];
	char ext[256];


	getFileTitle(filePath,FileName,ext);

	meta->Container=(char*)malloc(strlen(ext)+1);
	strcpy(meta->Container,ext);

	if(strcmp(ext,"mp4") == 0 || strcmp(ext,"mov") == 0 || strcmp(ext,"3gp") == 0    )
	{
		meta->title = (char*)malloc(strlen(FileName)+1);
		strcpy(meta->title,FileName);


//		printf("%s\n",ext);
		return parse_mp4(filePath,meta);

	}
	else if(strcmp(ext,"avi") == 0)
	{
		meta->title = (char*)malloc(strlen(FileName)+1);
		strcpy(meta->title,FileName);

		return parse_avi(filePath,meta);
	}else if(strcmp(ext,"wmv")==0)
	{
		meta->title = (char*)malloc(strlen(FileName)+1);
		strcpy(meta->title,FileName);

		return parse_wmv(filePath,meta);
	}else if(strcmp(ext,"mp3") == 0)
	{
		return parse_mp3(filePath,meta);
	}else if(strcmp(ext,"jpg") == 0)
	{
		meta->title = (char*)malloc(strlen(FileName)+1);
		strcpy(meta->title,FileName);

		return parse_jpg(filePath,meta);
	}
	else if(strcmp(ext,"png") == 0 )
	{
		meta->title = (char*)malloc(strlen(FileName)+1);
		strcpy(meta->title,FileName);

		return parse_png(filePath,meta);
	}


	return ret;

}


#ifndef ANDROID
int GenMetaData(char* filePath,char* fileURL)
#else
jstring Java_com_ht_dmc_devices_DeviceLocalPath_getMetadata(JNIEnv *env, jobject thiz, jstring _filePath, jstring _fileURL)
#endif
{
	struct Meta meta;
	int ret;

	char Duration[20];
	char MetaString[2048];
#ifdef ANDROID
	char *filePath = (char *)(*env)->GetStringUTFChars(env, _filePath, 0);
	char *fileURL = (char *)(*env)->GetStringUTFChars(env, _fileURL, 0);
#endif

//	LOGD("1 (%s, %s)", filePath, fileURL);

	memset(&meta,0,sizeof(struct Meta));

//	LOGD("2 (%s)", fileURL);

	ret = MediaParsing(filePath,&meta);


//	LOGD("3");

	printf("\n %d",ret);


	switch(ret)
	{
	case FILE_MOVIE:
//		LOGD("5");
		ConvertDuration(meta.Duration,Duration);
//		printf("\n%s\n",Duration);

		sprintf(MetaString,"<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\" xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\"> <item id=\"1\" parentID=\"0\" restricted=\"0\"><dc:title>%s</dc:title><dc:creator>UNKNOWN</dc:creator><upnp:artist>UNKNOWN</upnp:artist><upnp:album>UNKNOWN</upnp:album><upnp:genre>Unknown</upnp:genre><upnp:class>%s</upnp:class><res protocolInfo=\"%s\" resoution =\"%dx%d\" size=\"%d\" duration=\"%s\" bitrate=\"%d\" >%s</res></item></DIDL-Lite>",meta.title,meta.upnpClass,meta.ProtocolInfo,meta.ResolutionX,meta.ResolutionY,meta.fileSize,Duration,meta.fileSize/meta.Duration,fileURL);


/*
				"<res protocolInfo=\""+item.getFirstResource().getProtocolInfo()+"\" bitrate=\""+item.getFirstResource().getBitrate()+"\" duration=\""+item.getFirstResource().getDuration()+"\" resolution =\""+ item.getFirstResource().getResolutionX()+"x"+item.getFirstResource().getResolutionY()+"\" size=\""+item.getFirstResource().getSize()+"\" >"+item.getFirstResource().getValue()+"</res>" +

*/
		printf("\n %s \n",MetaString);

		break;

	case FILE_AUDIO:
//		LOGD("6");
		ConvertDuration(meta.Duration,Duration);
		sprintf(MetaString,"<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\" xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\"> <item id=\"1\" parentID=\"0\" restricted=\"0\"><dc:title>%s</dc:title><dc:creator>UNKNOWN</dc:creator><upnp:artist>UNKNOWN</upnp:artist><upnp:album>UNKNOWN</upnp:album><upnp:genre>Unknown</upnp:genre><upnp:class>%s</upnp:class><res protocolInfo=\"%s\" size=\"%d\" duration=\"%s\" bitrate=\"%d\" >%s</res></item></DIDL-Lite>",meta.title,meta.upnpClass,meta.ProtocolInfo,meta.fileSize,Duration,meta.Bitrate,fileURL);
		printf("\n %s \n",MetaString);
/*

		"<res protocolInfo=\""+meta.getMIME_TYPE()+"\" bitrate=\"44100\" duration=\""+meta.getDuration() +"\" size=\""+meta.getSize()+"\" >"+meta.getUrl()+"</res>" +

		*/

		break;
	case FILE_PICTURE:
	//	LOGD("7");
		sprintf(MetaString,"<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\" xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\"> <item id=\"1\" parentID=\"0\" restricted=\"0\"><dc:title>%s</dc:title><dc:creator>UNKNOWN</dc:creator><upnp:artist>UNKNOWN</upnp:artist><upnp:album>UNKNOWN</upnp:album><upnp:genre>Unknown</upnp:genre><upnp:class>%s</upnp:class><res protocolInfo=\"%s\" resoution =\"%dx%d\" size=\"%d\" >%s</res></item></DIDL-Lite>",meta.title,meta.upnpClass,meta.ProtocolInfo,meta.ResolutionX,meta.ResolutionY,meta.fileSize,fileURL);

		/*

		String meta = "<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\" xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\">" +
		"<item id=\""+item.getId()+"\" parentID=\""+item.getParentID()+"\" restricted=\""+(item.isRestricted()?"0":"1")+"\">" +
		"<dc:title>"+item.getTitle()+"</dc:title>" +
		"<dc:creator>"+item.getCreator()+"</dc:creator>" +
		"<upnp:artist>"+"UNKNOWN"+"</upnp:artist>" +
		"<upnp:album>UNKNOWN</upnp:album>" +
		"<upnp:genre>Unknown</upnp:genre>" +
		"<upnp:class>"+item.getClazz().getValue()+"</upnp:class>" +
		"<res protocolInfo=\""+item.getFirstResource().getProtocolInfo()+"\" resoution =\""+ item.getFirstResource().getResolutionX()+"x"+item.getFirstResource().getResolutionY()+"\" size=\""+item.getFirstResource().getSize()+"\" >"+item.getFirstResource().getValue()+"</res>" +
		"<upnp:class>"+item.getClazz().getValue()+"</upnp:class>" +
		"</item></DIDL-Lite> ";

		*/

		break;
	default:
		sprintf(MetaString, "");
	}
//	LOGD("8");

	if(meta.author != NULL) free(meta.author);
	if(meta.Container != NULL) free(meta.Container);
	if(meta.title != NULL) free(meta.title);
	if(meta.upnpClass != NULL) free(meta.upnpClass);
	if(meta.CodecBrand != NULL) free(meta.CodecBrand);
	if(meta.ProtocolInfo != NULL) free(meta.ProtocolInfo);
	if(meta.AUD_CODEC != NULL) free(meta.AUD_CODEC);
	if(meta.VID_CODEC != NULL) free(meta.VID_CODEC);

//	return (*env)->NewStringUTF(env, MetaString);

}
